package cn.tedu.springmvc2.controller;

import cn.tedu.springmvc2.dao.UserDao;
import cn.tedu.springmvc2.pojo.JsonResult;
import cn.tedu.springmvc2.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserDao dao;

    @PostMapping("user/register")
    public JsonResult doReg(@RequestBody User user) {
        user.setCreatedTime(new Date());
        user.setModifiedTime(new Date());
        dao.insert(user);
        return new JsonResult("register ok!");
    }

    /*
     * 查询注册时间比createdTime晚的用户
     */
    @GetMapping("user/list/{createdTime}")
    public JsonResult doList(@PathVariable String createdTime) {
        List<User> list = dao.list(createdTime);
        return new JsonResult(list);
    }

    /*
        修改用户的信息
     */
    @PutMapping("user/update")
    public JsonResult doUpdate(@RequestBody User user) {
        user.setModifiedTime(new Date());
        dao.update(user);
        return new JsonResult("update ok!");
    }
}
